Fk:loadTranslationTable{
  ["plum__lianjie"] = "连捷",
  [":plum__lianjie"] = "摸牌或出牌阶段结束后，若本阶段手牌数唯一最少的角色改变过，你可使用一张【杀】并执行另一个阶段。",

  ["#plum__lianjie-ivk"] = "连捷：可使用【杀】并执行%arg",

  ["@@plum__lianjie"] = "连捷"
}

local phase_tab = {
  [Player.Draw] = "phase_draw",
  [Player.Play] = "phase_play"
}

local lianjie = fk.CreateSkill{
  name = "plum__lianjie"
}

--- find mininate
---@param room Room
---@return integer[]
local findMin = function (room)
  local min = {}
  local n = 999
  for _, p in ipairs(room.alive_players) do
    if p:getHandcardNum() < n then
      min = {p.id}
      n = p:getHandcardNum()
    elseif p:getHandcardNum() == n then
      table.insert(min, p.id)
    end
  end
  return min
end

--- compare tables, was it changed?
---@param tab1 integer[]
---@param tab2 integer[]
---@return boolean
local compareTab = function (tab1, tab2)
  local n1, n2 = #tab1, #tab2
  local ck = {n1, n2, 1.5}
  table.sort(ck, function (a, b)
    return a > b
  end)
  if ck[2] == 1.5 then
    return true
  elseif ck[1] == 1.5 then
    return tab1[1] ~= tab2[1]
  else
    return false
  end
end

--- start mark
lianjie:addEffect(fk.EventPhaseStart, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(lianjie.name) and player == target then
      return table.contains({Player.Draw ,Player.Play}, data.phase)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "plum__lianjie", findMin(room))
  end
})

--- refresh mark
lianjie:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(lianjie.name) and table.contains({Player.Draw ,Player.Play}, player.phase)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if compareTab(player:getTableMark("plum__lianjie"), findMin(player.room)) then
      room:setPlayerMark(player, "plum__lianjie", 0)
      room:setPlayerMark(player, "@@plum__lianjie", 1)
    end
  end
})

lianjie:addEffect(fk.EventPhaseEnd, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(lianjie.name) and player == target and table.contains({Player.Draw ,Player.Play}, data.phase) and player:getMark("@@plum__lianjie") == 1 then
      player.room:setPlayerMark(player, "@@plum__lianjie", 0)
      return true
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local other = Player.Draw + Player.Play - data.phase
    local use = room:askToUseCard(player, {
      pattern = "slash",
      skill_name = lianjie.name,
      cancelable = true,
      prompt = "#plum__lianjie-ivk:::" .. phase_tab[other],
      extra_data = { bypass_times = true }
    })
    if not use then return end
    event:setCostData(self, {use = use})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self).use ---@type UseCardDataSpec
    if not use then return end
    room:useCard(use)
    if player.dead then return end
    local other = Player.Draw + Player.Play - data.phase
    player:gainAnExtraPhase(other, lianjie.name, true)
  end
})

return lianjie